<?php
/*
 * 计算经纬度范围
 * $lat 纬度
 * $lon 经度
 * $raidus 半径(米)
 */

function getAround($lat,$lon,$raidus){
    //计算纬度
    $PI = 3.14159265;
    $degree = (24901*1609)/360.0;
    $dpmLat = 1/$degree;
    $radiusLat = $dpmLat*$raidus;
    $minLat = $lat - $radiusLat;
    $maxLat = $lat + $radiusLat;
    $mpdLng = $degree*cos($lat * ($PI/180));
    $dpmLng = 1 / $mpdLng;
    $radiusLng = $dpmLng*$raidus;
    $minLng = $lon - $radiusLng;
    $maxLng = $lon + $radiusLng;
    //范围
    $range = array(
        'minLat' => $minLat,
        'maxLat' => $maxLat,
        'minLng' => $minLng,
        'maxLng' => $maxLng
    );
    return $range;
}

function array_sort($arr,$shortKey,$short=SORT_ASC,$shortType=SORT_REGULAR)
{
    foreach ($arr as $key => $data){
        $name[$key] = $data[$shortKey];
    }
    array_multisort($name,$short,$shortType,$arr);
    return $arr;
}

 function match_chinese($chars,$encoding='utf8')
    {
        $pattern =($encoding=='utf8')?'/[\x{4e00}-\x{9fa5}a-zA-Z0-9]/u':'/[\x80-\xFF]/';
        preg_match_all($pattern,$chars,$result);
        $temp =join('',$result[0]);
        return $temp;
    }




function getDistance($longitude1, $latitude1, $longitude2, $latitude2, $unit=2, $decimal=2)
{

    $EARTH_RADIUS = 6370.996; // 地球半径系数
    $PI = 3.1415926;

    $radLat1 = $latitude1 * $PI / 180.0;
    $radLat2 = $latitude2 * $PI / 180.0;

    $radLng1 = $longitude1 * $PI / 180.0;
    $radLng2 = $longitude2 * $PI / 180.0;

    $a = $radLat1 - $radLat2;
    $b = $radLng1 - $radLng2;

    $distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
    $distance = $distance * $EARTH_RADIUS * 1000;

    if ($unit == 2) {
        $distance = $distance / 1000;
    }

    return round($distance, $decimal);
}
 function getDistance2($lat1, $lng1, $lat2, $lng2){ 
        
             $EARTH_RADIUS =6378.137; // 地球半径系数
   			 $PI = 3.1415926535898;

         $radLat1 = $lat1 * ($PI / 180);
        $radLat2 = $lat2 * ($PI / 180);
         
         $a = $radLat1 - $radLat2; 
         $b = ($lng1 * ($PI / 180)) - ($lng2 * ($PI / 180)); 
        
       $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1)*cos($radLat2)*pow(sin($b/2),2))); 
        $s = $s * $EARTH_RADIUS; 
         $s = round($s * 100) / 100; 
         return $s; 
     }
function getDistance3($lat1=0,$lng1=0,$lat2=0,$lng2=0)
{
	$ak = '1PBbL6MvGkLogc8pbGVW7DSNLR9tjaIf';//您的百度地图ak，可以去百度开发者中心去免费申请  
	$distance = array();
	$distance['distance'] = 0.00;//距离 公里
	$distance['duration'] = 0.00;//时间 分钟
	$url = 'http://api.map.baidu.com/routematrix/v2/driving?output=json&origins='.$lat1.','.$lng1.'&destinations='.$lat2.','.$lng2.'&ak='.$ak;
	$data = file_get_contents($url);
	$data = json_decode($data,true);
	if (!empty($data) && $data['status'] == 0) {
		$distance['distance'] =preg_replace('/[^\.0123456789]/s', '', $data['result'][0]['distance']['value'])/1000; //计算距离
        $distance['distance'] = round( $distance['distance'] * 100) / 100; 
	}
	return $distance['distance'];
}
